/*
 * $Id: ItemOrderComparator.java 15 2013-05-16 05:43:09Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao.data.util;

import java.util.Comparator;

import ru.olympus.antonyhands.ao.data.entities.AOItem;

/**
 * @author AntonyHands
 * @since 29.02.2012
 */
public class ItemOrderComparator implements Comparator<AOItem> {

	private EItemsViewType _sortingType;

	public ItemOrderComparator(EItemsViewType sortingType)
	{
		_sortingType = sortingType;
	}

	@Override
	public int compare(AOItem arg0, AOItem arg1) {
		if (EItemsViewType.CRAFTABLE.equals(_sortingType) ||
				EItemsViewType.CRAFTABLE_WITH_CRAFT_LEVEL.equals(_sortingType))
			return getCraftLevel(arg0).compareTo(getCraftLevel(arg1));
		else
			return arg0.getTitle().compareTo(arg1.getTitle());
	}

	private Long getCraftLevel(AOItem item) {
		if (item.getCraftInfo() == null)
			return new Long(-1);
		return item.getCraftInfo().getCraftLevel();
	}
}
